﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using Dienthoai.Entities;
using Dienthoai.Data;

#endregion

namespace Dienthoai.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="OrdersOrderProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class OrdersOrderProviderBaseCore : EntityProviderBase<Dienthoai.Entities.OrdersOrder, Dienthoai.Entities.OrdersOrderKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByProductsIdFromOrdersDetails
		
		/// <summary>
		///		Gets Orders_Order objects from the datasource by Products_Id in the
		///		Orders_Details table. Table Orders_Order is related to table Products_Product
		///		through the (M:N) relationship defined in the Orders_Details table.
		/// </summary>
		/// <param name="_productsId"></param>
		/// <returns>Returns a typed collection of OrdersOrder objects.</returns>
		public TList<OrdersOrder> GetByProductsIdFromOrdersDetails(System.Int32 _productsId)
		{
			int count = -1;
			return GetByProductsIdFromOrdersDetails(null,_productsId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets Dienthoai.Entities.OrdersOrder objects from the datasource by Products_Id in the
		///		Orders_Details table. Table Orders_Order is related to table Products_Product
		///		through the (M:N) relationship defined in the Orders_Details table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_productsId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of OrdersOrder objects.</returns>
		public TList<OrdersOrder> GetByProductsIdFromOrdersDetails(System.Int32 _productsId, int start, int pageLength)
		{
			int count = -1;
			return GetByProductsIdFromOrdersDetails(null, _productsId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets OrdersOrder objects from the datasource by Products_Id in the
		///		Orders_Details table. Table Orders_Order is related to table Products_Product
		///		through the (M:N) relationship defined in the Orders_Details table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_productsId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Orders_Order objects.</returns>
		public TList<OrdersOrder> GetByProductsIdFromOrdersDetails(TransactionManager transactionManager, System.Int32 _productsId)
		{
			int count = -1;
			return GetByProductsIdFromOrdersDetails(transactionManager, _productsId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets OrdersOrder objects from the datasource by Products_Id in the
		///		Orders_Details table. Table Orders_Order is related to table Products_Product
		///		through the (M:N) relationship defined in the Orders_Details table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_productsId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Orders_Order objects.</returns>
		public TList<OrdersOrder> GetByProductsIdFromOrdersDetails(TransactionManager transactionManager, System.Int32 _productsId,int start, int pageLength)
		{
			int count = -1;
			return GetByProductsIdFromOrdersDetails(transactionManager, _productsId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets OrdersOrder objects from the datasource by Products_Id in the
		///		Orders_Details table. Table Orders_Order is related to table Products_Product
		///		through the (M:N) relationship defined in the Orders_Details table.
		/// </summary>
		/// <param name="_productsId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Orders_Order objects.</returns>
		public TList<OrdersOrder> GetByProductsIdFromOrdersDetails(System.Int32 _productsId,int start, int pageLength, out int count)
		{
			
			return GetByProductsIdFromOrdersDetails(null, _productsId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets Orders_Order objects from the datasource by Products_Id in the
		///		Orders_Details table. Table Orders_Order is related to table Products_Product
		///		through the (M:N) relationship defined in the Orders_Details table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_productsId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of OrdersOrder objects.</returns>
		public abstract TList<OrdersOrder> GetByProductsIdFromOrdersDetails(TransactionManager transactionManager,System.Int32 _productsId, int start, int pageLength, out int count);
		
		#endregion GetByProductsIdFromOrdersDetails
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, Dienthoai.Entities.OrdersOrderKey key)
		{
			return Delete(transactionManager, key.OrderId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_orderId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _orderId)
		{
			return Delete(null, _orderId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_orderId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _orderId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Orders_Order_Members key.
		///		FK_Orders_Order_Members Description: 
		/// </summary>
		/// <param name="_membersId"></param>
		/// <returns>Returns a typed collection of Dienthoai.Entities.OrdersOrder objects.</returns>
		public TList<OrdersOrder> GetByMembersId(System.Int32 _membersId)
		{
			int count = -1;
			return GetByMembersId(_membersId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Orders_Order_Members key.
		///		FK_Orders_Order_Members Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_membersId"></param>
		/// <returns>Returns a typed collection of Dienthoai.Entities.OrdersOrder objects.</returns>
		/// <remarks></remarks>
		public TList<OrdersOrder> GetByMembersId(TransactionManager transactionManager, System.Int32 _membersId)
		{
			int count = -1;
			return GetByMembersId(transactionManager, _membersId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Orders_Order_Members key.
		///		FK_Orders_Order_Members Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_membersId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Dienthoai.Entities.OrdersOrder objects.</returns>
		public TList<OrdersOrder> GetByMembersId(TransactionManager transactionManager, System.Int32 _membersId, int start, int pageLength)
		{
			int count = -1;
			return GetByMembersId(transactionManager, _membersId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Orders_Order_Members key.
		///		fkOrdersOrderMembers Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_membersId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Dienthoai.Entities.OrdersOrder objects.</returns>
		public TList<OrdersOrder> GetByMembersId(System.Int32 _membersId, int start, int pageLength)
		{
			int count =  -1;
			return GetByMembersId(null, _membersId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Orders_Order_Members key.
		///		fkOrdersOrderMembers Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_membersId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Dienthoai.Entities.OrdersOrder objects.</returns>
		public TList<OrdersOrder> GetByMembersId(System.Int32 _membersId, int start, int pageLength,out int count)
		{
			return GetByMembersId(null, _membersId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Orders_Order_Members key.
		///		FK_Orders_Order_Members Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_membersId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of Dienthoai.Entities.OrdersOrder objects.</returns>
		public abstract TList<OrdersOrder> GetByMembersId(TransactionManager transactionManager, System.Int32 _membersId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override Dienthoai.Entities.OrdersOrder Get(TransactionManager transactionManager, Dienthoai.Entities.OrdersOrderKey key, int start, int pageLength)
		{
			return GetByOrderId(transactionManager, key.OrderId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_Orders_Order index.
		/// </summary>
		/// <param name="_orderId"></param>
		/// <returns>Returns an instance of the <see cref="Dienthoai.Entities.OrdersOrder"/> class.</returns>
		public Dienthoai.Entities.OrdersOrder GetByOrderId(System.Int32 _orderId)
		{
			int count = -1;
			return GetByOrderId(null,_orderId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Orders_Order index.
		/// </summary>
		/// <param name="_orderId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Dienthoai.Entities.OrdersOrder"/> class.</returns>
		public Dienthoai.Entities.OrdersOrder GetByOrderId(System.Int32 _orderId, int start, int pageLength)
		{
			int count = -1;
			return GetByOrderId(null, _orderId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Orders_Order index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_orderId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Dienthoai.Entities.OrdersOrder"/> class.</returns>
		public Dienthoai.Entities.OrdersOrder GetByOrderId(TransactionManager transactionManager, System.Int32 _orderId)
		{
			int count = -1;
			return GetByOrderId(transactionManager, _orderId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Orders_Order index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_orderId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Dienthoai.Entities.OrdersOrder"/> class.</returns>
		public Dienthoai.Entities.OrdersOrder GetByOrderId(TransactionManager transactionManager, System.Int32 _orderId, int start, int pageLength)
		{
			int count = -1;
			return GetByOrderId(transactionManager, _orderId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Orders_Order index.
		/// </summary>
		/// <param name="_orderId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="Dienthoai.Entities.OrdersOrder"/> class.</returns>
		public Dienthoai.Entities.OrdersOrder GetByOrderId(System.Int32 _orderId, int start, int pageLength, out int count)
		{
			return GetByOrderId(null, _orderId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Orders_Order index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_orderId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="Dienthoai.Entities.OrdersOrder"/> class.</returns>
		public abstract Dienthoai.Entities.OrdersOrder GetByOrderId(TransactionManager transactionManager, System.Int32 _orderId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;OrdersOrder&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;OrdersOrder&gt;"/></returns>
		public static TList<OrdersOrder> Fill(IDataReader reader, TList<OrdersOrder> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				Dienthoai.Entities.OrdersOrder c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("OrdersOrder")
					.Append("|").Append((System.Int32)reader[((int)OrdersOrderColumn.OrderId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<OrdersOrder>(
					key.ToString(), // EntityTrackingKey
					"OrdersOrder",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new Dienthoai.Entities.OrdersOrder();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.OrderId = (System.Int32)reader[((int)OrdersOrderColumn.OrderId - 1)];
					c.MembersId = (System.Int32)reader[((int)OrdersOrderColumn.MembersId - 1)];
					c.OrderDate = (System.DateTime)reader[((int)OrdersOrderColumn.OrderDate - 1)];
					c.OrderQuantity = (System.Int32)reader[((int)OrdersOrderColumn.OrderQuantity - 1)];
					c.OrderTotal = (System.Double)reader[((int)OrdersOrderColumn.OrderTotal - 1)];
					c.ShippingName = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingName - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingName - 1)];
					c.ShippingAddress = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingAddress - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingAddress - 1)];
					c.ShippingTel = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingTel - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingTel - 1)];
					c.ShippingCity = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingCity - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingCity - 1)];
					c.ShippingNotice = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingNotice - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingNotice - 1)];
					c.IsActive = (reader.IsDBNull(((int)OrdersOrderColumn.IsActive - 1)))?null:(System.Boolean?)reader[((int)OrdersOrderColumn.IsActive - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="Dienthoai.Entities.OrdersOrder"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="Dienthoai.Entities.OrdersOrder"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, Dienthoai.Entities.OrdersOrder entity)
		{
			if (!reader.Read()) return;
			
			entity.OrderId = (System.Int32)reader[((int)OrdersOrderColumn.OrderId - 1)];
			entity.MembersId = (System.Int32)reader[((int)OrdersOrderColumn.MembersId - 1)];
			entity.OrderDate = (System.DateTime)reader[((int)OrdersOrderColumn.OrderDate - 1)];
			entity.OrderQuantity = (System.Int32)reader[((int)OrdersOrderColumn.OrderQuantity - 1)];
			entity.OrderTotal = (System.Double)reader[((int)OrdersOrderColumn.OrderTotal - 1)];
			entity.ShippingName = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingName - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingName - 1)];
			entity.ShippingAddress = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingAddress - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingAddress - 1)];
			entity.ShippingTel = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingTel - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingTel - 1)];
			entity.ShippingCity = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingCity - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingCity - 1)];
			entity.ShippingNotice = (reader.IsDBNull(((int)OrdersOrderColumn.ShippingNotice - 1)))?null:(System.String)reader[((int)OrdersOrderColumn.ShippingNotice - 1)];
			entity.IsActive = (reader.IsDBNull(((int)OrdersOrderColumn.IsActive - 1)))?null:(System.Boolean?)reader[((int)OrdersOrderColumn.IsActive - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="Dienthoai.Entities.OrdersOrder"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="Dienthoai.Entities.OrdersOrder"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, Dienthoai.Entities.OrdersOrder entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.OrderId = (System.Int32)dataRow["Order_Id"];
			entity.MembersId = (System.Int32)dataRow["Members_Id"];
			entity.OrderDate = (System.DateTime)dataRow["Order_Date"];
			entity.OrderQuantity = (System.Int32)dataRow["Order_Quantity"];
			entity.OrderTotal = (System.Double)dataRow["Order_Total"];
			entity.ShippingName = Convert.IsDBNull(dataRow["Shipping_Name"]) ? null : (System.String)dataRow["Shipping_Name"];
			entity.ShippingAddress = Convert.IsDBNull(dataRow["Shipping_Address"]) ? null : (System.String)dataRow["Shipping_Address"];
			entity.ShippingTel = Convert.IsDBNull(dataRow["Shipping_Tel"]) ? null : (System.String)dataRow["Shipping_Tel"];
			entity.ShippingCity = Convert.IsDBNull(dataRow["Shipping_City"]) ? null : (System.String)dataRow["Shipping_City"];
			entity.ShippingNotice = Convert.IsDBNull(dataRow["Shipping_Notice"]) ? null : (System.String)dataRow["Shipping_Notice"];
			entity.IsActive = Convert.IsDBNull(dataRow["IsActive"]) ? null : (System.Boolean?)dataRow["IsActive"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="Dienthoai.Entities.OrdersOrder"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">Dienthoai.Entities.OrdersOrder Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, Dienthoai.Entities.OrdersOrder entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region MembersIdSource	
			if (CanDeepLoad(entity, "Members|MembersIdSource", deepLoadType, innerList) 
				&& entity.MembersIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.MembersId;
				Members tmpEntity = EntityManager.LocateEntity<Members>(EntityLocator.ConstructKeyFromPkItems(typeof(Members), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.MembersIdSource = tmpEntity;
				else
					entity.MembersIdSource = DataRepository.MembersProvider.GetByMembersId(transactionManager, entity.MembersId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'MembersIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.MembersIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.MembersProvider.DeepLoad(transactionManager, entity.MembersIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion MembersIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByOrderId methods when available
			
			#region ProductsIdProductsProductCollection_From_OrdersDetails
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<ProductsProduct>|ProductsIdProductsProductCollection_From_OrdersDetails", deepLoadType, innerList))
			{
				entity.ProductsIdProductsProductCollection_From_OrdersDetails = DataRepository.ProductsProductProvider.GetByOrdersIdFromOrdersDetails(transactionManager, entity.OrderId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ProductsIdProductsProductCollection_From_OrdersDetails' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.ProductsIdProductsProductCollection_From_OrdersDetails != null)
				{
					deepHandles.Add("ProductsIdProductsProductCollection_From_OrdersDetails",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< ProductsProduct >) DataRepository.ProductsProductProvider.DeepLoad,
						new object[] { transactionManager, entity.ProductsIdProductsProductCollection_From_OrdersDetails, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region OrdersDetailsCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<OrdersDetails>|OrdersDetailsCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'OrdersDetailsCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.OrdersDetailsCollection = DataRepository.OrdersDetailsProvider.GetByOrdersId(transactionManager, entity.OrderId);

				if (deep && entity.OrdersDetailsCollection.Count > 0)
				{
					deepHandles.Add("OrdersDetailsCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<OrdersDetails>) DataRepository.OrdersDetailsProvider.DeepLoad,
						new object[] { transactionManager, entity.OrdersDetailsCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the Dienthoai.Entities.OrdersOrder object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">Dienthoai.Entities.OrdersOrder instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">Dienthoai.Entities.OrdersOrder Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, Dienthoai.Entities.OrdersOrder entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region MembersIdSource
			if (CanDeepSave(entity, "Members|MembersIdSource", deepSaveType, innerList) 
				&& entity.MembersIdSource != null)
			{
				DataRepository.MembersProvider.Save(transactionManager, entity.MembersIdSource);
				entity.MembersId = entity.MembersIdSource.MembersId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region ProductsIdProductsProductCollection_From_OrdersDetails>
			if (CanDeepSave(entity.ProductsIdProductsProductCollection_From_OrdersDetails, "List<ProductsProduct>|ProductsIdProductsProductCollection_From_OrdersDetails", deepSaveType, innerList))
			{
				if (entity.ProductsIdProductsProductCollection_From_OrdersDetails.Count > 0 || entity.ProductsIdProductsProductCollection_From_OrdersDetails.DeletedItems.Count > 0)
				{
					DataRepository.ProductsProductProvider.Save(transactionManager, entity.ProductsIdProductsProductCollection_From_OrdersDetails); 
					deepHandles.Add("ProductsIdProductsProductCollection_From_OrdersDetails",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<ProductsProduct>) DataRepository.ProductsProductProvider.DeepSave,
						new object[] { transactionManager, entity.ProductsIdProductsProductCollection_From_OrdersDetails, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<OrdersDetails>
				if (CanDeepSave(entity.OrdersDetailsCollection, "List<OrdersDetails>|OrdersDetailsCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(OrdersDetails child in entity.OrdersDetailsCollection)
					{
						if(child.OrdersIdSource != null)
						{
								child.OrdersId = child.OrdersIdSource.OrderId;
						}

						if(child.ProductsIdSource != null)
						{
								child.ProductsId = child.ProductsIdSource.ProductsId;
						}

					}

					if (entity.OrdersDetailsCollection.Count > 0 || entity.OrdersDetailsCollection.DeletedItems.Count > 0)
					{
						//DataRepository.OrdersDetailsProvider.Save(transactionManager, entity.OrdersDetailsCollection);
						
						deepHandles.Add("OrdersDetailsCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< OrdersDetails >) DataRepository.OrdersDetailsProvider.DeepSave,
							new object[] { transactionManager, entity.OrdersDetailsCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region OrdersOrderChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>Dienthoai.Entities.OrdersOrder</c>
	///</summary>
	public enum OrdersOrderChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Members</c> at MembersIdSource
		///</summary>
		[ChildEntityType(typeof(Members))]
		Members,
		///<summary>
		/// Collection of <c>OrdersOrder</c> as ManyToMany for ProductsProductCollection_From_OrdersDetails
		///</summary>
		[ChildEntityType(typeof(TList<ProductsProduct>))]
		ProductsIdProductsProductCollection_From_OrdersDetails,
		///<summary>
		/// Collection of <c>OrdersOrder</c> as OneToMany for OrdersDetailsCollection
		///</summary>
		[ChildEntityType(typeof(TList<OrdersDetails>))]
		OrdersDetailsCollection,
	}
	
	#endregion OrdersOrderChildEntityTypes
	
	#region OrdersOrderFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;OrdersOrderColumn&gt;"/> class
	/// that is used exclusively with a <see cref="OrdersOrder"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class OrdersOrderFilterBuilder : SqlFilterBuilder<OrdersOrderColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the OrdersOrderFilterBuilder class.
		/// </summary>
		public OrdersOrderFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the OrdersOrderFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public OrdersOrderFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the OrdersOrderFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public OrdersOrderFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion OrdersOrderFilterBuilder
	
	#region OrdersOrderParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;OrdersOrderColumn&gt;"/> class
	/// that is used exclusively with a <see cref="OrdersOrder"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class OrdersOrderParameterBuilder : ParameterizedSqlFilterBuilder<OrdersOrderColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the OrdersOrderParameterBuilder class.
		/// </summary>
		public OrdersOrderParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the OrdersOrderParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public OrdersOrderParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the OrdersOrderParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public OrdersOrderParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion OrdersOrderParameterBuilder
	
	#region OrdersOrderSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;OrdersOrderColumn&gt;"/> class
	/// that is used exclusively with a <see cref="OrdersOrder"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class OrdersOrderSortBuilder : SqlSortBuilder<OrdersOrderColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the OrdersOrderSqlSortBuilder class.
		/// </summary>
		public OrdersOrderSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion OrdersOrderSortBuilder
	
} // end namespace
